package com.jeff.android.lab.module.widget;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author: duff
 * @date: 2019/1/10
 * @since: 1.0.0
 */
public class BottomSheetDialogFragment extends BaseFragment {

    @BindView(R.id.btn_bottom_sheet_dialog)
    Button mBtnBottomSheetDialog;

    @BindView(R.id.btn_bottom_sheet_dialog_fragment)
    Button mBtnBottomSheetDialogFragment;

    BottomSheetDialog mSheetDialog;

    @Override
    protected int getRootLayout() {
        return R.layout.widget_fragment_bottom_sheet;
    }

    @Override
    protected void initView() {

    }

    @OnClick(R.id.btn_bottom_sheet_dialog)
    public void showSheetDialog() {
        if (mSheetDialog == null) {
            // create bottom sheet dialog
            mSheetDialog = new BottomSheetDialog(getContext());
            mSheetDialog.setContentView(R.layout.widget_dialog_bottom_sheet);

            // set dialog background
            mSheetDialog.getDelegate().findViewById(R.id.design_bottom_sheet)
                    .setBackgroundColor(Color.TRANSPARENT);

            // behavior
            final FrameLayout frameLayout =
                    mSheetDialog.findViewById(R.id.design_bottom_sheet);
            frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    frameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    BottomSheetBehavior behavior = BottomSheetBehavior.from(frameLayout);

                    behavior.setPeekHeight(frameLayout.findViewById(R.id.iv_libai).getHeight());
                    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                        @Override
                        public void onStateChanged(View view, int i) {
                            Log.d("jeff", "onStateChanged:" + i);
                        }

                        @Override
                        public void onSlide(View view, float v) {
                            Log.d("jeff", "onSlide:" + v);
                        }
                    });
                    //调用behavior相关方法
                    frameLayout.forceLayout();
                }
            });

        }

        mSheetDialog.show();
    }

    @OnClick(R.id.btn_bottom_sheet_dialog_fragment)
    public void showSheetDialogFragment() {
        com.google.android.material.bottomsheet.BottomSheetDialogFragment fragment = new InternalSheetDialogFragment();
        fragment.show(getFragmentManager(), "tag");
    }

    public static class InternalSheetDialogFragment
            extends com.google.android.material.bottomsheet.BottomSheetDialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return super.onCreateDialog(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.widget_fragment_internal_sheet_dialog,
                    container, false);
            return view;
        }

        @Override
        public void onStart() {
            super.onStart();
            BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
            // set dialog background
            dialog.getDelegate().findViewById(R.id.design_bottom_sheet)
                    .setBackgroundColor(Color.TRANSPARENT);
            FrameLayout bottomSheet = dialog.getDelegate()
                    .findViewById(R.id.design_bottom_sheet);
            BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

}
